<?php

namespace app\stock\controller\manage;

use think\Controller;
use think\Request;

class Highsea extends Controller
{
    protected $BusinessModel = null;
    protected $AdminModel = null;
    protected $ReceiveModel=null;

    public function __construct(){
        parent::__construct();
        $this->BusinessModel = model('business.Business');
        $this->AdminModel = model('admin.Admin');
        $this->ReceiveModel=model('business.Receive');
    }

    public function index(){
        $adminid = $this->request->param('adminid', 0, 'trim');

        $admin = $this->AdminModel->find($adminid);

        if (!$admin) {
            $this->error('该管理员账号不存在');
        }

        if ($admin['status'] !== 'normal') {
            $this->error('该管理员账号已被禁用');
        }

        $list = $this->BusinessModel->with(['source'])->whereNull('adminid')->select();

        if($list){
            $this->success('查询成功', null, $list);
        }else{
            $this->error('查询失败!');
        }
    }

    public function info(){
        $businessid=$this->request->param('businessid',0,'trim');
        $adminid = $this ->request->param('adminid', 0, 'trim');

        $admin = $this->AdminModel->find($adminid);
        if(!$admin){
            $this->error('当前管理员不存在!');
        }

        if($admin['status']!=='normal'){
            $this->error('当前管理员账号已被禁用!');
        }

        $business = $this->BusinessModel->with(['source'])->find($businessid);

        if(!$businessid){
            $this->error('该用户不存在!');
        }

        $this->success('查询成功!', null, $business);
    }

    public function apply(){
        $busid=$this->request->param('busid',0,'trim');
        $adminid=$this->request->param('adminid',0,'trim');

        $admin=$this->AdminModel->find($adminid);

        if(!$admin){
            $this->error('当前管理员账号不存在!');
        }

        if($admin['status']!=='normal'){
            $this->error('当前管理员账号被禁用!');
        }

        $business=$this->BusinessModel->find($busid);

        if(!$business){
            $this->error('当前用户账号不存在!');
        }

        // 开启事务
        $this->BusinessModel->startTrans();
        $this->ReceiveModel->startTrans();

        $BusinessData=[
            'id'=>$busid,
            'adminid'=>$adminid
        ];

        $BusinessStatus=$this->BusinessModel->isUpdate(true)->save($BusinessData);

        if(!$BusinessStatus){
            $this->error('用户数据更新失败!');
        }

        $ReceiveData=[
            'applyid'=>$adminid,
            'status'=>'apply',
            'busid'=>$busid
        ];

        $ReceiveStutus=$this->ReceiveModel->validate('common/business/Receive')->save($ReceiveData);

        if(!$ReceiveStutus){
            $this->BusinessModel->rollback();
            $this->error($this->ReceiveModel->getError());
        }

        if($BusinessStatus===false||$ReceiveStutus===false){
            $this->BusinessModel->rollback();
            $this->ReceiveModel->rollback();
            $this->error('申请失败!');
        }else{
            $this->BusinessModel->commit();
            $this->ReceiveModel->commit();
            $this->success('申请成功!');
        }
    }
}
